package hu.codeguide;

import android.app.Activity;
import android.app.ProgressDialog;
import java.net.URL;

import android.os.AsyncTask;
import android.util.Log;
import hu.codeguide.xml.RssItem;
import hu.codeguide.xml.RssParser;
import java.util.List;

/**
 * @author David Csejtei, CodeGuide Team, http://codeguide.hu
 * @version 1.0
 */

public class Downloader extends AsyncTask<String, Integer,  List<RssItem>> {
    
    private URL url;
    private Activity activity;
    private ProgressDialog dialog;
    private int status;

    public Downloader(final Activity activity) {
        this.activity = activity;
        dialog = new ProgressDialog(activity);
    }
        
    @Override
    protected List<RssItem> doInBackground(String... urls) {

        try {
            // Create RSS reader
            RssParser rssReader = RssParser.getInstance(urls[0]);
            // Parse RSS, get items
            return rssReader.getItems();
        } catch (Exception e) {
            Log.e("RSSRESULT2", e.getMessage());
        }

        return null;
    }
	
    @Override
    protected void onPostExecute(List<RssItem> result) {               
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        //Log.e("RSSRESULT2","LOADED!");
    }

    @Override
    protected void onPreExecute() {
        if(dialog != null) {
            // Set the dialog preferences
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setTitle("File downloading...");
            dialog.setIndeterminate(false);
            dialog.setProgress(0);
            dialog.show();
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        dialog.setProgress(values[0]);
    }
	
}